package com.jh.tools.base;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jh.tools.base.util.TUtil;

/**
 * Created by niejiahuan on 2016/5/12.
 * Fragment父类
 */
public abstract class BaseFragment <T extends BaseFPresenter,E extends BaseModel>extends Fragment{

    protected View baseView;
    public T mPresenter;//UI控制层
    public E mModel;//数据操作层
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (null==baseView){
            if (getLayoutId()!=0){
                baseView = inflater.inflate(getLayoutId(), container, false);
                mPresenter = TUtil.getT(this, 0);
                mModel = TUtil.getT(this, 1);
                this.initPresenter();
                this.initView(baseView);
            }else{
                throw new RuntimeException(
                        "Class need add LayoutId");
            }
        }
        return baseView;
    }
    /**
     * 获取布局文件
     * @return
     */
    public abstract int getLayoutId();

    /**
     * 初始化视图操作
     */
    public abstract void initView(View baseView);

    /**
     * 简单页面无需mvp就不用管此方法即可,完美兼容各种实际场景的变通
     */
    public abstract void initPresenter();
    /**
     * 获取视图依赖的activity
     * @return
     */
    public Activity getBaseActivty(){
        return this.getActivity();
    }
}
